<?php
/**
 *  * 系统-受国家计算机软件著作权保护 - !
 * =========================================================
 * Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.ohyu.cn
 * 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * User: ohyueo
 * Date: 2021/5/25
 * Time: 11:02
 */
namespace app\handler;

use app\api\models\EscortUserList;
use app\api\models\EscortPayIncome;
use app\api\models\EscortPayRefund;
use think\facade\Db;
use think\facade\Log;

class MoneyLogHandler
{
    /**
     * 加入记录
     *
     * @param User $user
     * @param      $money
     * @param int  $motype
     * @param      $type
     * @param      $text
     */
    public static function add($user, $money, $motype = 1, $type=1, $text='',$giving=0)
    {   
        $uid=$user->id;
        $user=EscortUserList::where('id',$uid)->find();
        $user->moneyLogs()->save([
            'user_id' => $user->id,
            'money' => $money,
            'giving' => $giving,
            'type' => $type,
            'mo_type' => $motype,
            'text' => $text,
            'addtime' => gettime(),
            'yue' => $user->money
        ]);
    }
    public static function addint($user, $money, $motype = 1, $type=1, $text='')
    {   
        $uid=$user->id;
        $user=EscortUserList::where('id',$uid)->find();
        $user->UserIntegral()->save([
            'user_id' => $user->id,
            'money' => $money,
            'type' => $type,
            'mo_type' => $motype,
            'text' => $text,
            'addtime' => gettime(),
            'yue' => $user->integral
        ]);
    }

    //收入明细
    /**
     * 
     * @param mixed $text 
     * @param mixed $money 
     * @param mixed $payst 
     * @param mixed $type 
     * @param mixed $orderid 
     * @param mixed $uid 
     * @param int $listid 
     * @param int $classid 
     * @param int $usertype 
     * @param int $ordtype 订单类型  1预约订单 2商品订单 3会员卡订单
     * @return void 
     */
    public static function addincome($text, $money, $orderid, $uid,$usertype=1){
        $merid='';
        $nianfen='';
        $jidu='';
        $schoolid='';
        $gradeid='';
        $classid='';
        $studentid='';
        if($orderid){
            //根据订单id获取订单信息
            $order=Db::name('schoolcatering_order_orderlist')->where('id',$orderid)->find();
            if(!$order){
                return false;
            }
            $merid=$order['merchant'];
            $nianfen=$order['nianfen'];
            $jidu=$order['jidu'];
            $schoolid=$order['schoolid'];
            $gradeid=$order['gradeid'];
            $classid=$order['classid'];
            $studentid=$order['studentid'];
        }
        $arrs=array(
            'title' => $text,
            'paymo' => $money,
            'orderid' => $orderid,
            'uid' => $uid,
            'addtime' => gettime(),
            'usertype' => $usertype,
            'userid' => $uid,
            'merid' => $merid,
            'nianfen' => $nianfen,
            'jidu' => $jidu,
            'schoolid' => $schoolid,
            'gradeid' => $gradeid,
            'classid' => $classid,
            'studentid' => $studentid,
        );
        EscortPayIncome::create($arrs);
    }
    
    //退款记录  记录普通的微信支付的退款记录
    public static function refundmsg($data){
        if(isset($data['payid']) && $data['payid']){
            $payid=$data['payid']; //支付订单id
            $pay=Db::name('schoolcatering_pay_payorder')->where('id',$payid)->find();
            if(!$pay){
                return false;
            }
            $type=$pay['type'];//支付方式 1微信 2支付宝 3银联
            $paytime=$pay['addtime'];
            $paymo=$pay['paymo'];
            $orderid=$data['orderid'];
            $res=Db::name('mall_order_orderlist')->where('id',$orderid)->cache()->find();
            $zmo=$res['money'];
            $merchant=$res['merchant'];
            $nianfen=$res['nianfen'];
            $jidu=$res['jidu'];
            $schoolid=$res['schoolid'];
            $gradeid=$res['gradeid'];
            $classid=$res['classid'];
            $studentid=$res['studentid'];
            $tuitype=1;
        }else{
            $payid=0;
            $type=0;
            $classid=0;
            $paytime=gettime();
            $orderid=$data['orderid'];
            $res=Db::name('mall_order_orderlist')->where('id',$orderid)->cache()->find();
            $zmo=$res['money'];
            $merchant=$res['merchant'];
            $nianfen=$res['nianfen'];
            $jidu=$res['jidu'];
            $schoolid=$res['schoolid'];
            $gradeid=$res['gradeid'];
            $classid=$res['classid'];
            $studentid=$res['studentid'];
            $paymo=$zmo;
            $tuitype=2;
        }
        $arrs=array(
            'title' => $data['title'],
            'orderid' => $data['orderid'],
            'uid' => $data['uid'],
            'type' => $type,
            'paytime' => $paytime,
            'tuitime' => gettime(),
            'tuimsg' => $data['tuimsg'],
            'usertype' => $data['usertype'],
            'userid' => $data['userid'],
            'payid' => $payid,
            'money' => $zmo,
            'paymo' => $paymo,
            'tuitype' => $tuitype,
            'merid' => $merchant,
            'classid' => $classid,
            'nianfen' => $nianfen,
            'jidu' => $jidu,
            'schoolid' => $schoolid,
            'gradeid' => $gradeid,
            'studentid' => $studentid,
        );
        EscortPayRefund::insert($arrs);
    }
}